package com.ar.network;

import java.util.ArrayList;

import org.apache.http.RequestLine;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * The Class RequestBuilder.
 * @author anh
 */
public class RequestBuilder {

	/**
	 * Instantiates a new request builder object.
	 */
	public RequestBuilder() {
		
	}
	
	/**
	 * Builds the on_login request.
	 *
	 * @param phoneNumber the phone number
	 * @param IMEI the IMEI of this phone
	 * @return the request as string 
	 */
	public static String BuildOnLoginRequest(String phoneNumber, String IMEI) {
		JSONObject request = new JSONObject();
		try {
			request.put(NetworkUtility.HANDLE, NetworkUtility.ONLOGIN);
			request.put(NetworkUtility.PHONE_NUMBER, phoneNumber);
			request.put(NetworkUtility.IMEI, IMEI);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return request.toString();
	}
	
	/**
	 * Builds the on_load difficult request.
	 *
	 * @param categoryId id of the desired category
	 * @return the request as string 
	 */
	public static String BuildOnLoadDifficultRequest(String categoryId){
		JSONObject request = new JSONObject();
		try {
			request.put(NetworkUtility.HANDLE, NetworkUtility.ONLOAD_DIFFICULT);
			request.put(NetworkUtility.CATEGORY_ID, categoryId);
		} catch (JSONException e) {
			
			e.printStackTrace();
		}
		return request.toString();
	}
	
	/**
	 * Builds the on_load request.
	 *
	 * @param categoryId id of the desired category
	 * @param categoryType type of the desired category
	 * @return the request as string 
	 */
/*	public static String BuildOnLoadRequest(String categoryId, String difficultId) {
		JSONObject request = new JSONObject();
		JSONObject request_load = new JSONObject();
		try {
			request_load.put(NetworkUtility.CATEGORY_ID, categoryId);
			request_load.put(NetworkUtility.DIFFICULT_ID, difficultId);
			request.put(NetworkUtility.HANDLE, NetworkUtility.ONLOAD);
			request.put(NetworkUtility.REQUEST_LOAD, request_load);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return request.toString();
	}*/
	
	public static String BuildOnLoadRequest(String categoryId, String difficultId) {
		JSONObject request = new JSONObject();
		try {
			request.put(NetworkUtility.HANDLE, NetworkUtility.ONLOAD);
			request.put(NetworkUtility.CATEGORY_ID, categoryId);
			request.put(NetworkUtility.CATEGORY_TYPE, difficultId);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return request.toString();
	}
	
	/**
	 * Builds the on_check request.
	 *
	 * @param quizId the list of all quizId
	 * @param answerId the array of all answerId
	 * @return the request as string
	 */
	public static String BuildOnCheckRequest(ArrayList<Integer> quizId, int[] answerId) {
		JSONObject request = new JSONObject();
		JSONArray results = new JSONArray();
		JSONArray choose;
		JSONObject jsonObject;
		try {
			for (int i = 0; i < quizId.size(); i++) {
				jsonObject = new JSONObject();
				choose = new JSONArray();
				choose.put(answerId[i]);
				jsonObject.put(NetworkUtility.ID, quizId.get(i));
				jsonObject.put(NetworkUtility.CHOOSE, choose);
				results.put(jsonObject);
			}
			request.put(NetworkUtility.HANDLE, NetworkUtility.ONCHECK);
			request.put(NetworkUtility.RESULTS, results);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return request.toString();
	}


}
